using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace DirectoryWatcher
{
    /// <summary>
    /// Represents a set of files that we should watch for changes, matching with either a wildcard
    /// expression or a regex.
    /// </summary>
    public class FileSetToWatch : ConfigurationElement
    {
        /// <summary>
        /// The Regex object for the match expression (if MatchExpressionType is 
        /// RegularExpression).
        /// </summary>
        private Regex matchRegEx = null;

        /// <summary>
        /// The expression to use to match against filenames to determine which ones we are 
        /// concerned with.
        /// </summary>
        [ConfigurationProperty("matchExpression", DefaultValue = "*")]
        public string MatchExpression
        {
            get
            {
                return (string)base["matchExpression"];
            }
        }

        /// <summary>
        /// The Regex object for the match expression (if MatchExpressionType is 
        /// RegularExpression).
        /// </summary>
        public Regex MatchRegex
        {
            get
            {
                // Instantiate the object only once, and only do so if MatchExpressionType is 
                // RegularExpression
                if (MatchExpressionType == MatchExpressionType.RegularExpression && matchRegEx == null)
                    matchRegEx = new Regex(MatchExpression);

                return matchRegEx;
            }
        }

        /// <summary>
        /// The type (either wildcard or regex) of the match expression.
        /// </summary>
        [ConfigurationProperty("matchExpressionType", DefaultValue = MatchExpressionType.Wildcard)]
        public MatchExpressionType MatchExpressionType
        {
            get
            {
                return (MatchExpressionType)base["matchExpressionType"];
            }
        }

        /// <summary>
        /// Collection of programs to execute when a file has changed.
        /// </summary>
        [ConfigurationProperty("programsToExecute", IsRequired = true)]
        public ProgramToExecuteCollection ProgramsToExecute
        {
            get
            {
                return (ProgramToExecuteCollection)base["programsToExecute"];
            }
        }

        /// <summary>
        /// Filesystem event types that we should watch for when monitoring file changes.
        /// </summary>
        [ConfigurationProperty("eventsToWatch", IsRequired = true)]
        public EventToWatchCollection EventsToWatch
        {
            get
            {
                return (EventToWatchCollection)base["eventsToWatch"];
            }
        }
    }
}
